package md.mru.frontend.models;

import md.mru.core.dtos.TreeNodeModelDto;
import org.primefaces.model.TreeNode;

import java.util.Collections;
import java.util.Comparator;

/**
 * @author sergiu.marin
 * @since 01/09/2014.
 */
public class SortableDefaultTreeNode extends DefaultTreeNode {

    private Comparator<TreeNode> TREE_NODE_COMPARATOR = new Comparator<TreeNode>() {
        @Override
        public int compare(TreeNode node1, TreeNode node2) {
            Double position1 = ((TreeNodeModelDto) node1.getData()).getPosition();
            Double position2 = ((TreeNodeModelDto) node2.getData()).getPosition();
            return position1.compareTo(position2);
        }
    };

    public void sort() {
        Collections.sort(super.getChildren(), TREE_NODE_COMPARATOR);
        for (TreeNode child : super.getChildren()) {
           //child.getChildren().sort();
        }
    }
}
